+-2sigma.Rmdのロジックを良くしたい
やりたいこと
mm/ddのリターンが過去〇〇日のリターンの-2σ以上なのか以下なのか判別したい
# rm(tmp)
# rm(tmp1)
# rm(tmp2)
# rm(touraku2)
#過去1週間分を評価する
#結果テーブル準備
# tmp2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# tmp2 <- as.Date(tmp2[,1])
# touraku2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# touraku2[,1] <- as.Date(touraku2[,1])
tmp2 <- dailyrtn
tmp2 <- transform(tmp2, sigma=0)
tmp2 <- transform(tmp2, buysign='--')
tmp2[,1] <- ymd(tmp2[,1])
tmp2[,6] <- as.character(tmp2[,6])
tmp2 <- tmp2[-1,]
touraku2 <- head(dailyrtn,1)
touraku2 <- transform(touraku2, sigma=0)
touraku2 <- transform(touraku2, buysign='--')
touraku2[,1] <- ymd(touraku2[,1])
touraku2[,6] <- as.character(touraku2[,6])
touraku2 <- touraku2[-1,]
for (i in 1:length(ticker_tmp)){
tmp <- subset(dailyrtn, ticker == ticker_tmp[i])
#列追加
tmp <- transform(tmp, sigma=0)
tmp <- transform(tmp, buysign='--')
tmp$date <- ymd(tmp$date)
tmp[,6] <- as.character(tmp[,6])
tmp <- tmp[-1,]
#1週間前の位置 1週間前から現在までのcloseを評価するの意
tarRow1 <- nrow(tmp) - 6
for (k in tarRow1:nrow(tmp)) {
#過去300日分のリターンの-2σ計算
#tarRow2 <- k - 300
#過去300日分の抽出
#tmp1 <- subset(tmp,
# as.numeric(row.names(tmp)) >= tarRow2
# & row.names(tmp) <= tarRow1 )
tmp1 <- tmp[1:tarRow1,]
tmp1 <- tail(tmp1, 300)
#-2σ計算
under2sigma <- quantile(tmp1$rtn,c(0.02275))
#-2σの追記
tmp[k, 5] <- under2sigma
#リターンとunder2sigmaを比較してbuyサインを立てる
if (tmp[k, 4] < tmp[k, 5]) {
tmp[k, 6] <- 'buy'
} else {
tmp[k, 6] <- '-'
}
}
#結果をテーブルへ
tmp2 <- rbind(tmp2, tmp)
touraku2 <- subset(tmp2, buysign=='buy')
touraku2$ticker <- as.factor(touraku2$ticker)
#1週間以内でフィルタ
touraku2 <- touraku2 %>%
filter(date >= Sys.Date()-7)
}
datatable(touraku2,
filter = 'top',
style = 'bootstrap', class = 'table-bordered table-condensed',
extensions = 'ColReorder',
options = list(dom = 'Rlfrtip')
)
#write.table(touraku2,'touraku2.txt',sep="\t",quote=F,row.names = F)
ticker_bakusagari <- as.character(t(touraku2$ticker))
#購入済で下がった銘柄は?
ticker_kaimashikoho <- intersect(ticker_bakusagari, kounyu)
#購入済でない銘柄は?
#setdiff(a,b)はaにあってbにないものを抽出する
ticker_shinkikoho <- setdiff(ticker_bakusagari, kounyu)
tmp <- subset(kabu.com_list, kabu.com_list$ticker %in% ticker_bakusagari)
datatable(tmp,
filter = 'top',
style = 'bootstrap', class = 'table-bordered table-condensed',
extensions = 'ColReorder',
options = list(dom = 'Rlfrtip')
)
購入済で下がった銘柄
新規購入候補の銘柄
1566, 1322, 2040, 1498, 1654, 1495, 1659, 1389, 1560, 1656, 1349, 2522, 1551, 2519, 1483, 1484, 1481, 1470, 1464, 1593, 1591, 2526, 1474, 1599, 1592, 1492, 1493, 2033, 1554, 1658, 1387, 1478, 1477, 2520, 2514, 1657, 2513, 1550, 1680, 1653, 1399, 1391, 1386, 1652, 2518, 1479, 1393, 1545, 2046, 1598, 1324, 1555, 2065, 1494, 2044, 1655, 1557, 1547, 2521, 2045, 2515, 2043, 1348, 1475, 2524, 1473, 2523, 1585, 1586, 1621, 1628, 1624, 1632, 1631, 1619, 1622, 1629, 1630, 1626, 1617, 1620, 1623, 1625, 1633, 1568, 1651, 1584, 1546, 1679, 2050, 1541, 1385, 1326, 1540, 1542, 2517, 1597, 1476, 1595, 1398, 1488, 2516, 2042, 1615, 1698, 2037, 1682, 1346, 2525, 1369, 1397, 1578, 1570, 1458, 1579, 1489, 2066, 1497, 2067, 2529, 1480, 1660, 2048, 2068, 1577, 2069, 3487, 3281, 3227, 3290, 3279, 8975, 3463, 8963, 3298, 2971, 8954, 8958, 8972, 3278, 3453, 3488, 3473, 3459, 2972, 8985, 8987, 8952, 3468, 3492, 3451, 3295, 8956, 8964, 3455, 3470, 3466, 3493, 8977, 3471, 3478, 8961, 3234, 3287, 8984, 8976, 3476, 8957, 3226, 8951, 8955, 3308, 8953, 8967, 8986, 8968, 8966, 3462, 1365, 1320, 1321, 1329, 1358
#国内
if (length(intersect(touraku2$ticker, kokunaiETF)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
#海外
if (length(intersect(touraku2$ticker, kaigai)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
ggplotly(
ggplot(subset(monthlyrtn, ticker %in% ticker_bakusagari),
aes(x = return_monthly, colour = ticker)
) + geom_density()
)